<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../../asset/jquery-easyui-1.5.5.2/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../asset/jquery-easyui-1.5.5.2/themes/icon.css">
    <script type="text/javascript" src="../../asset/jquery-easyui-1.5.5.2/jquery.min.js"></script>
    <script type="text/javascript" src="../../asset/jquery-easyui-1.5.5.2/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../asset/jquery-easyui-1.5.5.2/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
        var url;

        function searchUser(){
            $('#dg').datagrid('load',{
                u_userName:$('#u_userName').val()
            });
        }

        function deleteUser(){
            var selectedRows=$("#dg").datagrid('getSelections');
            if(selectedRows.length==0){
                $.messager.alert("系统提示","请选择要删除的数据！");
                return;
            }
            var strIds=[];
            for(var i=0;i<selectedRows.length;i++){
                strIds.push(selectedRows[i].id);
            }
            var ids=strIds.join(",");
            $.messager.confirm("系统提示","您确认要删掉这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
                if(r){
                    $.post("UserDeleteAction.action",{delIds:ids},function(result){
                        if(result.success){
                            $.messager.alert("系统提示","您已成功删除<font color=red>"+result.delNums+"</font>条数据！");
                            $("#dg").datagrid("reload");
                        }else{
                            $.messager.alert('系统提示','<font color=red>'+selectedRows[result.errorIndex].userName+'</font>'+result.errorMsg);
                        }
                    },"json");
                }
            });
        }


        function openUserAddDialog(){
            resetValue();
            $("#dlg").dialog("open").dialog("setTitle","添加用户信息");
            url="UserSaveAction.action";
        }

        function openUserModifyDialog(){
            var selectedRows=$("#dg").datagrid('getSelections');
            if(selectedRows.length!=1){
                $.messager.alert("系统提示","请选择一条要编辑的数据！");
                return;
            }
            var row=selectedRows[0];
            $("#dlg").dialog("open").dialog("setTitle","编辑用户信息");
            // $("#fm").form("load",row);
            $("#userName").val(row.userName);
            $("#password").val(row.password);
            $("#sex").combobox("setValue",row.sex);
            $("#email").val(row.email);
            $("#birthday").datebox("setValue",row.birthday);
            $("#userDesc").val(row.userDesc);
            url="UserSaveAction.action?id="+row.id;
        }

        function closeUserDialog(){
            $("#dlg").dialog("close");
            resetValue();
        }

        function resetValue(){
            $("#userName").val("");
            $("#password").val("");
            $("#sex").combobox("setValue","")
            $("#email").val("");
            $("#birthday").datebox("setValue","");
            $("#userDesc").val("");
        }


        function saveUser(){
            $("#fm").form("submit",{
                url:url,
                onSubmit:function(){
                    return $(this).form("validate");
                },
                success:function(result){
                    if(result.errorMsg){
                        $.messager.alert("系统提示",result.errorMsg);
                        return;
                    }else{
                        $.messager.alert("系统提示","保存成功");
                        resetValue();
                        $("#dlg").dialog("close");
                        $("#dg").datagrid("reload");
                    }
                }
            });
        }


    </script>
</head>
<body style="margin: 5px;">
<table id="dg" title="用户信息" class="easyui-datagrid" fitColumns="true"
       pagination="true" rownumbers="true" url="UserFindAction.action" fit="true" toolbar="#tb">
    <thead>
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="30" align="center">用户ID</th>
        <th field="userName" width="80" align="center">用户名</th>
        <th field="password" width="80" align="center">密码</th>
        <th field="sex" width="30" align="center">性别</th>
        <th field="birthday" width="80" align="center">出生日期</th>
        <th field="email" width="80" align="center">邮箱</th>
        <th field="userDesc" width="80" align="center">备注</th>
    </tr>
    </thead>
</table>
<div id="tb">
    <div>
        <a href="javascript:openUserAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
        <a href="javascript:openUserModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
        <a href="javascript:deleteUser()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
    </div>
    <div>&nbsp;用户名：&nbsp;<input type="text" name="u_userName" id="u_userName"/><a href="javascript:searchUser()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a></div>
</div>
<div id="dlg" class="easyui-dialog" style="width: 800px;height: 350px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <table cellspacing="5px;">
            <tr>
                <td>用户名：</td>
                <td><input type="text" name="user.userName" id="userName" class="easyui-validatebox" required="true"/></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>密码：</td>
                <td><input type="text" name="user.password" id="password" class="easyui-validatebox" required="true"/></td>
            </tr>
            <tr>
                <td>性别：</td>
                <td><select class="easyui-combobox" id="sex" name="user.sex" editable="false" panelHeight="auto" style="width: 155px">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select></td>
                <td></td>
                <td>出生日期：</td>
                <td><input class="easyui-datebox" name="user.birthday" id="birthday" required="true" editable="false" /></td>
            </tr>
            <tr>
                <td>Email：</td>
                <td><input type="text" name="user.email" id="email" class="easyui-validatebox" required="true" validType="email"/></td>
            </tr>
            <tr>
                <td valign="top">备注：</td>
                <td colspan="4"><textarea rows="7" cols="50" name="user.userDesc" id="userDesc"></textarea></td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveUser()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeUserDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
</body>
</html>